package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompMchntPublicDealService;
import cc.rengu.igas.bomp.facade.request.*;
import cc.rengu.igas.bomp.facade.response.*;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by zhangxuran on 2020/04/02
 */
public class BompMchntPublicDealServiceImpl implements BompMchntPublicDealService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<MchntEntryDispatchResponse> mchntEntryDispatch(MchntEntryDispatchRequest mchntEntryDispatchRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntEntryDispatchResponse> result = new Result<>();
        MchntEntryDispatchResponse mchntEntryDispatchResponse = new MchntEntryDispatchResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntEntryDispatchRequest, MchntEntryDispatchResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntEntryDispatchResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户入驻派单通知处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntEntryDispatchResponse.setRespCode(((BizException) e).getCode());
                mchntEntryDispatchResponse.setRespDesc(e.getMessage());
            } else {
                mchntEntryDispatchResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntEntryDispatchResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<MchntBatchExportResponse> mchntBatchExport(MchntBatchExportRequest mchntBatchExportRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntBatchExportResponse> result = new Result<>();
        MchntBatchExportResponse mchntBatchExportResponse = new MchntBatchExportResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntBatchExportRequest, MchntBatchExportResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntBatchExportResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("间联商户批量导出数据处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntBatchExportResponse.setRespCode(((BizException) e).getCode());
                mchntBatchExportResponse.setRespDesc(e.getMessage());
            } else {
                mchntBatchExportResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntBatchExportResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<ParamQueryResponse> paramQuery(ParamQueryRequest paramQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ParamQueryResponse> result = new Result<>();
        ParamQueryResponse paramQueryResponse = new ParamQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(paramQueryRequest, ParamQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ParamQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("系统参数查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                paramQueryResponse.setRespCode(((BizException) e).getCode());
                paramQueryResponse.setRespDesc(e.getMessage());
            } else {
                paramQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                paramQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<MchntD0QueryResponse> mchntD0Query(MchntD0QueryRequest mchntD0QueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntD0QueryResponse> result = new Result<>();
        MchntD0QueryResponse mchntD0QueryResponse = new MchntD0QueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntD0QueryRequest, MchntD0QueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntD0QueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户是否能开通D0及提现查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntD0QueryResponse.setRespCode(((BizException) e).getCode());
                mchntD0QueryResponse.setRespDesc(e.getMessage());
            } else {
                mchntD0QueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntD0QueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<ReloadCacheResponse> reloadCache(ReloadCacheRequest reloadCacheRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ReloadCacheResponse> result = new Result<>();
        ReloadCacheResponse reloadCacheResponse = new ReloadCacheResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(reloadCacheRequest, ReloadCacheResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ReloadCacheResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("缓存刷新处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                reloadCacheResponse.setRespCode(((BizException) e).getCode());
                reloadCacheResponse.setRespDesc(e.getMessage());
            } else {
                reloadCacheResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                reloadCacheResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }


    @Override
    public Result<MchntBatchImportResponse> mchntBatchImport(MchntBatchImportRequest mchntBatchImportRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntBatchImportResponse> result = new Result<>();
        MchntBatchImportResponse mchntBatchImportResponse = new MchntBatchImportResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntBatchImportRequest, MchntBatchImportResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntBatchImportResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("间联商户导入操作处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntBatchImportResponse.setRespCode(((BizException) e).getCode());
                mchntBatchImportResponse.setRespDesc(e.getMessage());
            } else {
                mchntBatchImportResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntBatchImportResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
